import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getDepartmentsByUnitCode, getPostChangeStatus } from '/@/api/system/system';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { useMessage } from '/@/hooks/web/useMessage';

export const columns: BasicColumn[] = [
  {
    title: '排序号',
    dataIndex: 'orders',
    width: 70,
  },
  {
    title: '名称',
    dataIndex: 'name',
    width: 180,
  },
  {
    title: '部门',
    dataIndex: 'fullDeptName',
    width: 100,
  },
  {
    title: '限制人数',
    dataIndex: 'numLimited',
    width: 100,
  },
  {
    title: '成员',
    dataIndex: 'member',
    width: 150,
  },
  {
    title: '启用',
    dataIndex: 'status',
    width: 120,
    ellipsis: true,
    customRender: ({ record }: any) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }

      const newStatus = record.status;
      return h(Switch, {
        checked: record.status,
        checkedChildren: '是',
        unCheckedChildren: '否',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const { createMessage } = useMessage();
          getPostChangeStatus({ id: record.id, status: checked })
            .then((res) => {
              record.status = !record.status;
              createMessage.success(`操作成功`);
            })
            .catch(() => {
              record.status = newStatus;
              createMessage.error('操作失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '描述',
    dataIndex: 'description',
    width: 200,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '名称',
    component: 'Input',
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    colProps: {
      span: 24,
    },
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'deptCode',
    label: '部门',
    component: 'ApiTreeSelect',
    required: true,
    componentProps: {
      api: getDepartmentsByUnitCode,
      // multiple: true,
      // maxTagCount: 3,
      resultField: 'list',
      fieldNames: {
        label: 'name',
        key: 'code',
        value: 'code',
      },
      getPopupContainer: () => document.body,
    },
    colProps: { span: 24 },
  },
  {
    field: 'orders',
    label: '排序号',
    helpMessage: '排序号越大，排名越高',
    colProps: { span: 24 },
    required: true,
    component: 'InputNumber',
  },
  {
    field: 'name',
    label: '名称',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'description',
    label: '描述',
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'status',
    label: '启用',
    component: 'Switch',
    defaultValue: true,
    colProps: { span: 24 },
    componentProps: {
      checkedChildren: '是',
      unCheckedChildren: '否',
      checkedValue: true,
    },
  },
  {
    field: 'limited',
    label: '是否限制人数',
    component: 'Switch',
    colProps: { span: 24 },
    componentProps: {
      checkedChildren: '是',
      unCheckedChildren: '否',
      checkedValue: true,
    },
  },
  {
    field: 'numLimited',
    label: '限制人数',
    colProps: { span: 24 },
    component: 'InputNumber',
    helpMessage: '0或负值表示不限制',
    ifShow: ({ values }) => !!values.limited,
  },
  {
    field: 'excluded',
    label: '是否岗位互斥',
    component: 'Switch',
    colProps: { span: 24 },
    componentProps: {
      checkedChildren: '是',
      unCheckedChildren: '否',
    },
  },
  {
    field: 'postsExcluded',
    label: '岗位',
    component: 'ApiTreeSelect',
    slot: 'postsExcluded',
    // componentProps: {
    //   api: getPostList,
    //   multiple: true,
    //   maxTagCount: 3,
    //   resultField: 'list',
    //   fieldNames: {
    //     label: 'name',
    //     key: 'id',
    //     value: 'id',
    //   },
    //   getPopupContainer: () => document.body,
    // },
    colProps: { span: 24 },
    ifShow: ({ values }) => !!values.excluded,
  },
];
